package com.shanzhu.hospital.controller;

import com.shanzhu.hospital.utils.ApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@CrossOrigin("*")//用于启动跨域资源共享支持
public class AIController {//通过外部API获取AI生成的回复


    @Value("sk-a370613c8ca643bda1ffc1335c6d0e7d")//使用@value直接注入api密钥
    private String apiKey;

    @PostMapping("/chat")
    public ResponseEntity<String> chat(@RequestBody Map<String, String> request) {

        try {

            String response = ApiClient.getAIResponse(request.get("message"), apiKey);
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("{\"error\":\"API请求失败\"}");
        }
    }
}